package model;

import java.util.Vector;

import various.Constants;

/**
 * Classe permettant de charger toutes les extensions choisi en remplissant la pioche du jeu avec les différentes tuiles des différentes extensions
 * par défaut une intance de BaseExpansion est chargée
 * @author kn-cm
 *
 */
public class GameLoader {

	private Vector<AbstractFactoryExpansion> expansionList;
	private Vector<ModelPlayer> playerList;
	private final int MAXplayerNumbers = 6;
	private final int pawnNumbers = 9;
	private Vector<ModelPawn> pawnList;
	private ModelBoardCK board;
	private BaseExpansion minimum;
	public static int playerNb = 0;
	
	public GameLoader(ModelBoardCK b,Vector<AbstractFactoryExpansion> expList, BaseExpansion min)
	{
		this.board = b;
		this.expansionList = expList;
		this.minimum = min;
		//on ajoute l'extension de base (29 premieres tuiles)
		this.expansionList.add(this.minimum);
		this.playerList = new Vector<ModelPlayer>();
		this.pawnList = new Vector<ModelPawn>();
	}
	

	public GameLoader(ModelBoardCK b, BaseExpansion min)
	{
		this(b,new Vector<AbstractFactoryExpansion>(),min);
	}
	
	
	public void initBoard()
	{
		this.pawnList = this.minimum.makePawns();
		for(int i =0;i<this.expansionList.size();i++)
		{	//on genere les autres tuiles
			this.expansionList.get(i).makeTiles();
			
			//on met en place la premiere tuile
			this.board.setfirstTileImg(
					(this.board.getGrid().size()/Constants.vectorSide)/2, 
					(this.board.getGrid().size()/Constants.vectorSide)/2,
					this.expansionList.get(i));

			//on ajoute les piles genere a la board (i.e a la pile)
			this.board.fillStack(this.expansionList.get(i));
		}
//		THIS.BOARD.
	}
	
	public void addExpansion(AbstractFactoryExpansion expansion)
	{
		this.expansionList.add(expansion);
	}
	
	public void removeExpansion(AbstractFactoryExpansion expansion)
	{
		this.expansionList.remove(expansion);
	}


	public boolean addPlayer(ModelPlayer pl)
	{
		boolean check = false;
		if(this.playerList.size() < this.MAXplayerNumbers)
		{
			this.playerList.add(pl);
			ModelPlayerCK newPlayer = (ModelPlayerCK) this.playerList.lastElement(); 
			Vector<ModelPawn> hisPawns = new Vector<ModelPawn>();
			ModelPawnCK p;
			
			//on ajoute au nouveau joueur sa liste de pions
			//l'indice du pions dans la liste de pions correspond au nb de joueurs actuels
			for(int i=0; i<ModelPlayerCK.pawnNumbers;i++)
			{
				p = (ModelPawnCK) this.pawnList.get(GameLoader.playerNb);
				p.setPlayer(newPlayer);
				hisPawns.add(p);
			}
			
			((ModelPlayerCK)this.playerList.get(GameLoader.playerNb)).setPawns(hisPawns);
			GameLoader.playerNb++;
			check = true;
		}
		return check;
	}
	
	public void removePlayer(ModelPlayer pl)
	{
		this.playerList.remove(pl);
	}
	
	public void setBoard(ModelBoardCK board) {
		this.board = board;
	}


	public ModelBoardCK getBoard() 
	{
		return board;
	}
	
	public Vector<ModelPlayer> getPlayers()
	{
		return this.playerList;
	}
	
}
